Cumulative Plot Sales

Column

Plots Sold Across the World

Row

Plot Locations

Plots Sold in the US

World Plots

Column

Cumulative Plot Sales

country sold
United States 4327
United Kingdom 474
France 293
Italy 207
Australia 201
Switzerland 191
Japan 183
United Arab Emirates 164
Canada 139
Spain 128
China 111
Singapore 105
Germany 98
Netherlands 71
Hong Kong 69
Egypt 62
India 62
Mexico 62
Austria 54
Portugal 49
Russia 48
Brazil 44
South Korea 43
Israel 41
Monaco 39
Turkey 39
Peru 35
Greece 34
Thailand 34
Poland 33
Uzbekistan 31
Vatican City 31
Finland 30
Cambodia 28
Belgium 21
Saudi Arabia 21
Indonesia 20
New Zealand 15
Panama 15
Qatar 14
South Africa 13
Ukraine 13
coordinates 11
Czech Republic 10
Ireland 10
Colombia 9
Argentina 8
Philippines 8
NA 7
Iraq 6
Jamaica 5
Jordan 5
Malaysia 5
Zimbabwe 5
Denmark 4
Georgia 4
Malta 4
Romania 4
Venezuela 4
Bahamas 3
Cuba 3
Guatemala 3
Iceland 3
Kazakhstan 3
Saint Barthelemy 3
Saint Lucia 3
Taiwan 3
Vietnam 3
Armenia 2
Azerbaijan 2
Croatia 2
Dominican Republic 2
French Polynesia 2
Ghana 2
Jerusalem District 2
Nepal 2
Ontario 2
Palestinian Territories 2
Puerto Rico 2
Senegal 2
Bolivia 1
Bulgaria 1
Chad 1
Chile 1
Costa Rica 1
Cyprus 1
Estonia 1
Ethiopia 1
Hungary 1
Luxembourg 1
Mongolia 1
Montenegro 1
North Korea 1
Norway 1
Serbia 1
Sri Lanka 1
Sweden 1
US Virgin Islands 1

Column

Day

Week

Month

Year

Total

US Plots

Column

Cumulative US Plot Sales

state sold
California 849
Texas 744
New York 728
Florida 447
Nevada 322
Illinois 135
District of Columbia 103
Tennessee 82
Georgia 79
Massachusetts 75
Pennsylvania 61
Louisiana 57
Colorado 55
Washington 47
North Carolina 42
Kentucky 40
Hawaii 38
Michigan 36
Minnesota 36
Indiana 35
Ohio 35
Missouri 32
New Jersey 31
Arizona 29
Maryland 26
Wyoming 20
Wisconsin 19
Alabama 17
Oklahoma 17
Oregon 17
Virginia 17
Arkansas 12
Utah 12
South Carolina 8
Connecticut 4
Maine 4
New Mexico 3
South Dakota 3
Mississippi 2
Delaware 1
Iowa 1
Kansas 1
Nebraska 1
New Hampshire 1
New York L2GX5 1
North Dakota 1
Ontario 1

Column

Day

Week

Month

Year

Total

---
title: "SuperWorld Plot Sales"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    source_code: embed
    theme: yeti
---

Cumulative Plot Sales
=====================================

Inputs {.sidebar}
-------------------------------------

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
library(leaflet)
library(leaflet.extras)
library(sf)
library(tidyverse)
library(rnaturalearth)
library(rnaturalearthdata)
library(plotly)
library(usmap)
library(lubridate)

plots_sold = read_csv("C:/Users/rebec/SuperWorld_Plot_Recommendation/data/plots_sold.csv")[-1]
plots_sold$code = toupper(plots_sold$code)

us_plots = plots_sold[which(plots_sold$code == "US"),]
us_address = us_plots$address

state = c()
for (i in 1:length(us_address)){
  add = tail(unlist(str_split(us_address[i], pattern = ", ")), 2)[1]
  add = gsub(' [[:digit:]]+', '', add)
  state = c(state, add)
}

us_plots = cbind(us_plots, state) 

state_data = data.frame(state) %>%
  group_by(state) %>%
  summarise(sold = n())

```

*Total Plot Sales:*

```{r}
nrow(plots_sold)
```


*Top 10 Countries:* ```{r} plots_sold %>% group_by(country) %>% summarise(`plots sold` = n()) %>% arrange(-`plots sold`) %>% head(10) %>% knitr::kable() ```
*Top 10 US States:* ```{r} state_data %>% summarise(state, `plots sold` = sold) %>% arrange(-`plots sold`) %>% head(10) %>% knitr::kable() ``` Column {data-width=800} ------------------------------------- ### Plots Sold Across the World ```{r warning=FALSE, message=FALSE} world = ne_countries(scale = "medium", returnclass = "sf") df = st_sf(merge(plots_sold, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) df_plot = df %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) # df2 = df %>% # group_by(country, code) %>% # summarise(sold = n()) %>% # mutate(sold = ifelse(is.na(country), 0, sold)) # plot(df2["sold"], logz = TRUE, main = NULL, key.pos = 4) ggplotly(df_plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=-10, yshift=80, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` Row ------------------------------------- ### Plot Locations ```{r} leaflet(plots_sold) %>% addTiles() %>% addCircles(lng = ~lon, lat = ~lat) %>% setView(lat = 37.0902, lng = -95.7129, zoom = 4) ``` ### Plots Sold in the US ```{r} us = plot_usmap(data = state_data, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") ggplotly(us) ``` World Plots ===================================== Column {data-width=200} ------------------------------------- ### Cumulative Plot Sales ```{r} plots_sold %>% group_by(country) %>% summarize(sold = n()) %>% arrange(-sold) %>% knitr::kable() ``` Column {.tabset} ------------------------------------- ### Day ```{r} plots_today = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 1) df_today = st_sf(merge(plots_today, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_today %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradientn(colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE)) ``` ### Week ```{r} plots_week = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 7) df_week = st_sf(merge(plots_week, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_week %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), -Inf, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradientn(trans = "log", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=-10, yshift=80, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Month ```{r} plots_month = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 30) df_month = st_sf(merge(plots_month, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_month %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), -Inf, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradientn(trans = "log", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=-10, yshift=80, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Year ```{r} plots_year = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 365) df_year = st_sf(merge(plots_year, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_year %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), -Inf, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradientn(trans = "log", colors = colorspace::heat_hcl(5, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=-10, yshift=80, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Total ```{r} plot = df %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), -Inf, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradientn(trans = "log", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) + geom_sf_text(aes(label = code), size = 1) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=-10, yshift=80, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` US Plots ===================================== Column {data-width=200} ------------------------------------- ### Cumulative US Plot Sales ```{r} us_plots %>% group_by(state) %>% summarize(sold = n()) %>% arrange(-sold) %>% knitr::kable() ``` Column {.tabset} ------------------------------------- ### Day ```{r} us_today = us_plots %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 1) %>% group_by(state) %>% summarise(sold = n()) us_today = plot_usmap(data = us_today, values = "sold", regions = "states") + theme(legend.position = "right") + # scale_fill_continuous(name = "Plots Sold") + scale_fill_gradientn(name = "Plots Sold", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) ggplotly(us_today) ``` ### Week ```{r} us_week = us_plots %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 7) %>% group_by(state) %>% summarise(sold = n()) us_week = plot_usmap(data = us_week, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_gradientn(name = "Plots Sold", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) ggplotly(us_week) ``` ### Month ```{r} us_month = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 30) %>% group_by(state) %>% summarise(sold = n()) us_month = plot_usmap(data = us_month, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_gradientn(name = "Plots Sold", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) ggplotly(us_month) ``` ### Year ```{r} us_year = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 365) %>% group_by(state) %>% summarise(sold = n()) us_year = plot_usmap(data = us_year, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_gradientn(name = "Plots Sold", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) ggplotly(us_year) ``` ### Total ```{r} us = plot_usmap(data = state_data, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_gradientn(name = "Plots Sold", colors = colorspace::heat_hcl(12, h = c(-60, -150), l = c(75, 40), c = c(40, 80), power = 100)) ggplotly(us) ```